package com.future.mayi.qsale.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.adapter.BaseRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/19.
 */

public class SelectImgAdapter extends BaseRecyclerViewAdapter<String> {

    public static final String ADD_ITEM_STRING = "add";

    private int maxSelectNum = 12;
    private int itemHeight;


    public SelectImgAdapter(Context context, List<String> list) {
        super(context, list);
        this.itemHeight = (CommonUtils.getScreenSize(mContext)[0] - CommonUtils.dip2px(mContext, 32)) / 4;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view;
        view = getView(parent, R.layout.layout_publish_img_item);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemHeight, itemHeight);
        view.setLayoutParams(layoutParams);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            setItemValues((ItemViewHolder) holder, position);
        }
    }

    private void setItemValues(final ItemViewHolder holder, int position) {

        if (mOnItemClickListener != null) {
            holder.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getList().get(holder.getLayoutPosition()).toString().equals(ADD_ITEM_STRING))
                        mOnItemClickListener.OnItem(v, holder.getLayoutPosition());
                }
            });
        }

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getList().get(holder.getLayoutPosition()).toString().equals(ADD_ITEM_STRING)) {
                    delete(holder.getLayoutPosition());
                    if (getItemCount() < maxSelectNum && (!getList().get(getItemCount() - 1).toString().equals(ADD_ITEM_STRING))) {
                        List<String> list = new ArrayList<>();
                        list.add(ADD_ITEM_STRING);
                        addMore(list);
                    }
                }
            }
        });


        if (mList.get(position) != null) {
            if (mList.get(position).equals(ADD_ITEM_STRING)) {
                holder.ivDelete.setVisibility(View.GONE);
                holder.rlAddItem.setVisibility(View.VISIBLE);
                holder.tvAddTime.setText((mList.get(mList.size()-1).equals(ADD_ITEM_STRING) ? mList.size() - 1 : mList.size()) + "/" + maxSelectNum);
            } else {
                Glide.with(mContext).load(mList.get(position)).placeholder(R.color.black4).into(holder.ivImg);
                holder.ivDelete.setVisibility(View.VISIBLE);
                holder.rlAddItem.setVisibility(View.GONE);
            }
        } else {
            holder.ivImg.setImageResource(R.drawable.trans_bg);
        }

    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        @BindView(R.id.iv_add_item)
        ImageView ivAddItem;
        @BindView(R.id.rl_add_item)
        RelativeLayout rlAddItem;
        @BindView(R.id.ll_data)
        LinearLayout llData;
        @BindView(R.id.tv_add_item)
        TextView tvAddTime;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
